package com.zlc.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zlc.gulimall.product.dao.BrandDao;
import com.zlc.gulimall.product.dao.CategoryDao;
import com.zlc.gulimall.product.entity.BrandEntity;
import com.zlc.gulimall.product.service.BrandService;
import com.zlc.gulimall.product.vo.BrandRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlc.common.utils.PageUtils;
import com.zlc.common.utils.Query;

import com.zlc.gulimall.product.dao.CategoryBrandRelationDao;
import com.zlc.gulimall.product.entity.CategoryBrandRelationEntity;
import com.zlc.gulimall.product.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Resource
    BrandDao brandDao;

    @Resource
    CategoryDao categoryDao;

//    @Resource
//    BrandService brandService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetails(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        String brandName = brandDao.selectById(brandId).getName();
        String categoryName = categoryDao.selectById(catelogId).getName();
        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelation.setCatelogName(categoryName);

        this.save(categoryBrandRelation);
    }


    @Override
    public List<BrandEntity> queryByCatId(Long catId) {
        //获取分类与品牌的关联
        List<CategoryBrandRelationEntity> list = this.list(new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                .eq(CategoryBrandRelationEntity::getCatelogId, catId));

        //获取品牌
        List<BrandEntity> result = list.stream().map((item) -> {
            Long brandId = item.getBrandId();
            return brandDao.selectById(brandId);
        }).collect(Collectors.toList());

        //返回
        return result;
    }

    @Override
    public List<BrandRespVo> transToBrandRespVo(List<BrandEntity> brands) {
            return brands.stream().map((item) -> {
            BrandRespVo brandRespVo = new BrandRespVo();
            brandRespVo.setBrandId(item.getBrandId());
            brandRespVo.setBrandName(item.getName());
            return brandRespVo;
        }).collect(Collectors.toList());
    }

    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        this.update(categoryBrandRelationEntity,
                new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }
}